/************************************************************
Copyright (C) Xiong,Hui at www.51embed.com
FileName: main.c
Author: Xiong,Hui huixiong73@gmail.com
Date:08/3/12
Description:  the loop of process
Version: 1.0
Function List: 
    1. main();
History: 
    1. Huixiong 08/3/12 1.0 build this moudle
	2. Huixiong 08/3/17 1.1 add the 7-segment led display process 
***********************************************************/
#include <absacc.h>
#include <intrins.h>
#include <string.h>
#include "at89x52.H"
#include "proc.h"
#include "GUI.h"

const unsigned char HZT[]={0x24,0x24,0xA4,0XFE,0XA3,0X22,0X20,0X7E,
0X42,0X42,0X42,0X42,0x42,0x7F,0X02,0X00,
0X08,0X06,0X01,0XFE,0X00,0X43,0X41,0X49,
0X49,0x49,0X7F,0X49,0X4D,0X69,0X41,0X00,
};

#if 0
struct EDIT_Obj_struct {
  //WIDGET Widget;
  char * hpText;
  I16 MaxLen;
  U16 BufferSize;
  I32 Min, Max;            /* Min max values as normalized floats (integers) */
  U8 NumDecs;              /* Number of decimals */
  U32 CurrentValue;        /* Current value */
  int CursorPos;           /* Cursor position. 0 means left most */
  //unsigned SelSize;        /* Number of selected characters */
  U8 EditMode;             /* Insert or overwrite mode */
  //U8 XSizeCursor;          /* Size of cursor when working in insert mode */
  U8 Flags;
  tEDIT_AddKeyEx     * pfAddKeyEx;     /* Handle key input */
  tEDIT_UpdateBuffer * pfUpdateBuffer;  /* Update textbuffer */
  //EDIT_PROPS Props;
  //#if GUI_DEBUG_LEVEL >= GUI_DEBUG_LEVEL_CHECK_ALL
    //int DebugId;
  //#endif  
};
#endif

char Test_Buf[10] ={0};
extern void DecMode_AddKey(EDIT_Obj * pObj, int Key);
extern void DecMode_UpdateBuffer(EDIT_Obj * pObj);

struct EDIT_Obj_struct Dec_Edit =
{
  Test_Buf,
  8,
  sizeof(Test_Buf),
  -999999, 999999,
  1,
  -1000,
  0,
  GUI_EDIT_MODE_OVERWRITE,
  0,
  &DecMode_AddKey,
  &DecMode_UpdateBuffer
};


LISTBOX_Obj Listbox =
{
  0,
  "a\0b\0c\0d",
  4
};

DIALOG_Item Dialog_Item[] =
{
 {EDIT_T, "Input", 0,0, 64, 0, &Dec_Edit, &Dec_Edit},
 {LISTBOX_T, "Type", 0, 16, 32, 16, &Listbox, &Listbox }

};

DIALOG_Obj Dialog =
{
0, Dialog_Item, 0, 2, 0

};
/*************************************************************
*  Function: main()
*  Description: the main loop function which call 
     task process routines 
*  Calls:
	timer_500ms(),key_pro(),uart_rece(),
	uart_send(),lcd_disp(),led_disp()
*  Called by: none
*  Input:
*  Output:
*  Return:
*  Others:
*  History:
   1. Huixiong 08/3/12 build this moudle
   2. Huixiong 08/3/17 add led_disp() for 7-seg led display
   3. Huixiong 08/4/17 add lcd_pro() for lcd display
   4. Huixiong 08/4/18 add iic_pro() for 7-seg led display
*************************************************************/
void _main(void)
{
	EA = 0;					 //disable the gloable interrupt again
	mcu_init();				//initialize the SFR of MCU 
	sys_init();				//set the Variables of system
	EA = 1;					//enable the global interrupt flag
    
    OS_Init();
	//OS_Put_Char('a');
    OS_Debug_Print("start!!!\r\n");
	while(1){
		//timer_500ms();	     //process every 500ms
		//key_pro();			 //process the key which had been pushed
		uart_rece();		 //process the data received by serial communication
		//uart_pro();		 //prepare the data that would be sent by serial communication
		lcd_pro();			 //128*64 LCD display 
		//led_disp();			 //4 bits 7-segment LED display 
		//iic_pro();
	}
}

/*************************************************************
*  Function: timer_500ms()
*  Description: 500ms' routine
*  Calls: none
*  Called by: main()
*  Input:
     1.10msflag which is set by timer0's interrupt every 10 ms  
*  Output:
*  Return:
*  Others:
*  Updated:
      1. Huixiong 08/3/13 created
*************************************************************/
void timer_500ms(void)
{
	if(timer10msflag == TRUE){
		timer10msflag = FALSE;
		if(++counter >= 50){
			LED = !LED;	        //flash the running led every 500ms
			counter = 0;
		}
	}
}
/*************************************************************
*  Function: led_disp()
*  Description:4bits-7segment led display ,a test routine
*  Calls:
*  Called by:
*  Input:
*  Output:
*  Return:
*  Others:
*  History:
	1. Huixiong 08/3/17 created
*************************************************************/
void led_disp(void)
{
    if(testledflag == 1){	   //if the flag is on(1),the led display 
	    testledflag = 0;
		led[0] =1;		   //the data will be display on the 7-segment led
	    led[1] =2;
	    led[2] =3;
	    led[3] =4;
	}
}
/*************************************************************
*  Function: lcd_disp()
*  Description:12864 lcd display
*  Calls:
*  Called by: main()
*  Input:
*  Output:
*  Return:
*  Others:
*  History:
	1. Huixiong 08/4/17 created
*************************************************************/
extern void Disp_String_At(char Str[],char X,char Y,char Disp_Flag);
extern void EDIT_Disp(EDIT_Obj *pObj, U8 Cursor, U8 X, U8 Y);
void lcd_pro(void)
{
  static char a = 0;
/*
 EDIT_AddKey(&Dec_Edit, GUI_Key);
 EDIT_Disp(&Dec_Edit, 1, 0, 0);
 GUI_Key = 0;
*/
/*
 LISTBOX_AddKey(&Listbox, GUI_Key);
 LISTBOX_Disp(&Listbox, 1, 0, 0);
 GUI_Key = 0;
*/
 if(a == 0)
 {
  DIALOG_CreateEx(&Dialog, Dialog_Item, 2);
  a = 1;
 }
 DIALOG_AddKey(&Dialog, GUI_Key);
 DIALOG_Disp(&Dialog);

 GUI_Key = 0;
}

/*************************************************************
*  Function:iic_pro()
*  Description:
*  Calls:
*  Called by: main()
*  Input:
*  Output:
*  Return:
*  Others:
*  History:
   1. Huixiong 08/4/17 created
*************************************************************/
void iic_pro(void)
{
    i2cstrin[0] = 6;
	i2cstrin[1] = 7;
	i2cstrin[2] = 8;
	i2cstrin[3] = 9;
	i2cstrout[0] = 0;
	if(testiicflag){
        testiicflag = 0;
		i2cwrite(0x00,0x08,4); 
		delay(1);
		i2cread(0x00,0x08,4);
		led[0] =  i2cstrout[0];
		led[1] =  i2cstrout[1];
		led[2] =  i2cstrout[2];
		led[3] =  i2cstrout[3];
	 }
}
/*************************************************************
*  Function: uart_pro()
*  Description:	test the uart
*  Calls:
*  Called by:
*  Input:
*  Output:
*  Return:
*  Others:
*  History:
*************************************************************/
void uart_pro(void)
{
    unsigned char code *codeptr;
	unsigned char xdata *dataptr;
	if(uartproflag == TRUE){
		uartproflag = FALSE;
		codeptr = &welcome;
		dataptr	= &uartsendbuf;
		strcpy(dataptr,codeptr);
		uartsendptr=sizeof(welcome);
		uartsendtempptr = 0x00;
		uartsendflag = TRUE;
		TI = 1;						//intiates the sending
	}
}